import schedule
import subprocess
import time
from datetime import date

def job1():
    subprocess.call('scrapy crawl ad_account_info')
    subprocess.call('scrapy crawl ad_plan_agg_data')

def job2():
    subprocess.Popen('scrapy crawl ad_plan_agg_data_by_hour')

def job3():
    '''
    月数据任务, 每月的1号0点运行
    :return:
    '''
    today = date.today()
    if today.day == 1:
        subprocess.Popen('scrapy crawl ad_month_info')
    else:
        pass

def job4():
    subprocess.call('scrapy crawl ad_account_info')
    subprocess.call('scrapy crawl ad_finance_flow')

def job5():
    subprocess.Popen('scrapy crawl ad_plan_data_by_hour_single -a account_type=yfd')

def job6():
    subprocess.Popen('scrapy crawl ad_plan_data')
    subprocess.Popen('scrapy crawl ad_placement_data_single')

def job7():
    subprocess.Popen('scrapy crawl ad_put_data_by_hour_single')

def job8():
    subprocess.call('scrapy crawl ad_material_data')
    subprocess.call('scrapy crawl ad_img_material_data')

def job9():
    subprocess.Popen('scrapy crawl ad_transfer_flow')

def job10():
    subprocess.Popen('scrapy crawl ad_plan_data_by_hour_single -a account_type=ybc')

if __name__=='__main__':
    print('开始运行广点通爬虫定时器...')
    # 日终余额
    schedule.every().day.at("00:00").do(job4)
    # 日消耗数据
    schedule.every().day.at("08:30").do(job1)
    # 计划日报数据
    schedule.every().day.at("05:30").do(job6)
    # 素材日报数据
    schedule.every().day.at("08:00").do(job8)
    # 转账流水数据
    schedule.every().day.at("10:00").do(job9)

    # 投放分时数据
    schedule.every().day.at("07:50").do(job2)
    schedule.every().day.at("09:50").do(job2)
    schedule.every().day.at("10:50").do(job2)
    schedule.every().day.at("11:50").do(job2)
    schedule.every().day.at("13:50").do(job2)
    schedule.every().day.at("14:50").do(job2)
    schedule.every().day.at("15:50").do(job2)
    schedule.every().day.at("17:50").do(job2)
    schedule.every().day.at("19:50").do(job2)
    schedule.every().day.at("21:50").do(job2)

    # 计划分时数据 - 猿辅导
    # schedule.every().day.at("09:45").do(job5)
    # schedule.every().day.at("11:45").do(job5)
    # schedule.every().day.at("14:45").do(job5)
    # schedule.every().day.at("17:45").do(job5)
    # schedule.every().day.at("19:45").do(job5)


    # 计划分时数据 - 猿编程
    # schedule.every().day.at("09:48").do(job10)
    # schedule.every().day.at("11:48").do(job10)
    # schedule.every().day.at("14:48").do(job10)
    # schedule.every().day.at("17:48").do(job10)
    # schedule.every().day.at("21:48").do(job10)

    # 投放分时数据-单AD路径
    # schedule.every().day.at("09:50").do(job7)
    # schedule.every().day.at("13:50").do(job7)
    # schedule.every().day.at("17:50").do(job7)

    while True:
        schedule.run_pending()
        time.sleep(1)